package com.example.home.fragment;

import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.home.R;
import com.example.home.adapter.CarAdapter;
import com.example.home.bean.NewsBean;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import static com.example.home.tool.StaticActi.ip;

public class HelpFragment extends Fragment {


    private View view;
    private Handler handler;
    private OkHttpClient okHttpClient;
    private NewsBean help1;
    private RecyclerView rcv_new_help;
    CarAdapter carAdapter;
    EditText search_et;
    String userInput;
    private TextView search_btn;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_help, container, false);

        initview();
        okHttpClient = new OkHttpClient(); // 初始化okHttpClient
        //全部服务图标
        getCarData();

        // 初始化 carAdapter 并与 RecyclerView 关联
        carAdapter = new CarAdapter(getContext(), new ArrayList<>());
        rcv_new_help.setAdapter(carAdapter);

        userInput = search_et.getText().toString();
        onQueryTextChange();
        return view;
    }


    private boolean onQueryTextChange() {
        search_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 获取当前输入的文本
                userInput = search_et.getText().toString();
                //0:判断文本长度0：退出搜素，长度大于0的时候，进入搜索
                if (TextUtils.isEmpty(userInput)) {
                    carAdapter.setList(help1.getRows());
                } else {
                    //1比对，生成一个新的list
                    List<NewsBean.RowsBean> list = new ArrayList<>();
                    for (NewsBean.RowsBean newsBean : help1.getRows()) {
                        if (newsBean.getServiceName().toLowerCase().contains(userInput)) {
                            list.add(newsBean);
                        }
                    }
                    //2把新的list设置给adapter，然后刷新。
                    carAdapter.setList(list);
                }
                carAdapter.notifyDataSetChanged();
            }
        });
        return true;
    }


    private void getCarData() {
        Request request = new Request.Builder().url(ip + "/prod-api/api/service/list").build();
        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                e.printStackTrace();
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                Gson gson = new Gson();
                String json = response.body().string();
                help1 = gson.fromJson(json, NewsBean.class);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        bindCarData();
                    }
                });
            }
        });
    }

    private void bindCarData() {
        List<NewsBean.RowsBean> rows = help1.getRows();
        NewsBean.RowsBean rowsDTO = new NewsBean.RowsBean();
        rowsDTO.setServiceName("律师");
        rowsDTO.setId(35);
        rowsDTO.setImgUrl("/prod-api/profile/upload/image/2023/02/13/081db43b-4b53-4261-968e-b75c52477f63.png");

        //预约检车

        NewsBean.RowsBean rowsDTO2 = new NewsBean.RowsBean();
        rowsDTO2.setServiceName("预约检车");
        rowsDTO2.setId(88);
        rowsDTO2.setImgUrl("/prod-api/profile/upload/image/2023/02/13/081db43b-4b53-4261-968e-b75c52477f63.png");

        rows.add(rowsDTO);
        rows.add(rowsDTO2);

        carAdapter = new CarAdapter(getContext(), help1.getRows());
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 5);
        rcv_new_help.setLayoutManager(gridLayoutManager);
        rcv_new_help.setAdapter(carAdapter);
    }

    private void initview() {
        okHttpClient = new OkHttpClient();
        handler = new Handler();
        rcv_new_help = view.findViewById(R.id.rcv_new_help);
        search_et = view.findViewById(R.id.search_et);
        search_btn = view.findViewById(R.id.search_btn);
    }

}